package com.emergency.embackend.controller;

import com.emergency.embackend.common.PageParams;
import com.emergency.embackend.common.R;
import com.emergency.embackend.param.ArticleDataParam;
import com.emergency.embackend.param.ArticleParam;
import com.emergency.embackend.service.OperateService;
import com.emergency.embackend.service.WikiArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("wiki/article")
@Slf4j
public class WikiArticleController {
    @Autowired
    private WikiArticleService wikiArticleService;
    @Autowired
    private OperateService operateService;

    @PostMapping("/getArticle")
    public R getArticle(@RequestBody ArticleDataParam articleDataParam){
        if(articleDataParam == null){
            return R.error("参数为空");
        }
        return R.ok().put("data",wikiArticleService.getArticleData(articleDataParam));
    }

    @GetMapping("/getOne")
    public R getOne( @RequestParam("articleId") Long articleId){
        if(articleId==null){
            return R.error(500,"请求参数出错");
        }
        return R.ok().put("data",wikiArticleService.getOne(articleId));
    }


    @GetMapping("/delete")
    public R delete(@RequestParam("articleId") Long articleId){
        if(articleId==null){
            return R.error(500,"请求参数出错");
        }
        wikiArticleService.delete(articleId);
        return R.ok();
    }

    @PostMapping("/update")
    public R update(@RequestBody ArticleParam articleParam){
        if(articleParam == null){
            return R.error(500,"参数不允许为空");
        }
        wikiArticleService.update(articleParam);
        return R.ok();
    }

    @PostMapping("/add")
    public R add(@RequestBody ArticleParam articleParam){
        if(articleParam == null){
            return R.error(500,"参数不允许为空");
        }
        wikiArticleService.add(articleParam);
        return R.ok();
    }

    @PostMapping("/getRecentArticle")
    public R getRecentArticle(@RequestBody PageParams pageParams) {
        return R.ok().put("data", wikiArticleService.getArticlesByCreateTimeDesc(pageParams));
    }

    @GetMapping("/like")
    public R like(@RequestParam("articleId") Long articleId){
        Integer isLike = operateService.like(articleId,'1');
        return R.ok().put("data",isLike);
    }
    @GetMapping("/collect")
    public R collect(@RequestParam("articleId") Long articleId){
        Integer isCollect = operateService.collect(articleId,'1');
        return R.ok().put("data",isCollect);
    }

    @GetMapping("/getHotArticle")
    public R getHotArticle(){
        return R.ok().put("data",wikiArticleService.getHotArticle());
    }

    @PostMapping("/getRecommendArticle")
    public R getRecommendArticle(@RequestBody ArticleDataParam articleDataParam){
        return R.ok().put("data",wikiArticleService.getRecommendArticle(articleDataParam));
    }
}
